//
//  FPPesonRegisViewController.m
//  ForestPack
//
//  Created by jimmy on 2018/7/4.
//  Copyright © 2018年 郑洲. All rights reserved.

//  选择个人或企业认证和选择身份后进入的注册界面

#import "FPPesonRegisViewController.h"
#import "FPRegisAndForgetView.h"
#import "FPThirdView.h"
#import "UIView+Utils.h"
#import "FPCertificateViewController.h"
#import "FPLoginViewController.h"
#import "FPSelectRoleViewController.h"
#import "FPRegisterViewController.h"

@interface FPPesonRegisViewController ()

@property (nonatomic,strong)  UILabel      * textLabel;
@property (nonatomic,strong)  UIButton     * loginButton;
@property (nonatomic,strong)  UIButton     * nextButton;
@property (nonatomic,strong)  UIImageView  * progressView;
@property (nonatomic,assign)  BOOL           is_Person;
@property (nonatomic,strong)  FPRegisAndForgetView   *regisView;


@end

@implementation FPPesonRegisViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"个人"];
    
    _is_Person = NO;
    [self setTopViewUI];
    [self setPersonalRegisterUI];
    [self setBottomViewUI];
    
}

-(void)backClick
{
    [self.navigationController popViewControllerAnimated:true];
}

#pragma mark - 点击进入认证界面
-(void)nextClick:(UIButton *)sender
{
    NSString * phoneStr = self.regisView.phoneNumTextField.text;
    NSString * password = self.regisView.passwordTextField.text;
    NSString * confirmPassword = self.regisView.confirmPasswordTextField.text;
    NSString * code = self.regisView.messageTextField.text;
    
    if (phoneStr.length <= 0) {
        [HttpManager showNoteMsg:@"输入手机号码有误"];
        return;
    }
    if (![FPLoginAndRegisterTools checkYzmField:code]) {
        [HttpManager showNoteMsg:@"验证码错误"];
        return;
    }
    if (![FPLoginAndRegisterTools checkPassword:password]) {
        [HttpManager showNoteMsg:@"请输入6-12位的数字和字母"];
        return;
    }
    if (![password isEqualToString:confirmPassword]) {
        [HttpManager showNoteMsg:@"两次输入的密码不相同"];
        return;
    }
    
    [self register_Code_Validate];
//    [self to_personalRegister];
}

-(void)register_Code_Validate
{
    NSString * url = [NSString stringWithFormat:@"%@app/register_validate",REQUESTHEADER];
    NSDictionary * dict = @{@"mobile":self.regisView.phoneNumTextField.text,@"code":self.regisView.messageTextField.text};
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {

            [self to_personalRegister];

        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];

}

- (void)to_personalRegister
{
    NSString * phoneStr = self.regisView.phoneNumTextField.text;
    NSString * password = self.regisView.passwordTextField.text;
    NSString * vertifyCode = self.regisView.messageTextField.text;
    
    FPCertificateViewController *certifiVC = [[FPCertificateViewController alloc] init];
    certifiVC.registerType = self.registerType;
    certifiVC.phoneStr = phoneStr;
    certifiVC.password = password;
    certifiVC.vertifyCode = vertifyCode;
    [self.navigationController pushViewController:certifiVC animated:true];
}

-(void)getData
{
    NSString *url = @"";
    NSDictionary * parameter = @{};
    
    url = [NSString stringWithFormat:@"%@app/app_personal_register",REQUESTHEADER];//个人用户注册
    parameter = @{@"mobile":self.regisView.phoneNumTextField.text,@"password":self.regisView.passwordTextField.text};

    [HttpManager postHttpRequestByPost:url andParameter:parameter success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            kNSUDefaultSaveVauleAndKey(successResponse[@"token"], @"token");
            kNSUDefaultSaveVauleAndKey(self.regisView.phoneNumTextField.text, @"phone");
//            kNSUDefaultSaveVauleAndKey(successResponse[@"customer_type"], @"customer_type");
           
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
    
}

#pragma mark - 点击进入登录界面
-(void)LoginClick
{
    FPLoginViewController *loginVC = [[FPLoginViewController alloc] init];
    [self.navigationController pushViewController:loginVC animated:true];
}

#pragma mark - 设置头部View
-(void)setTopViewUI
{
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(13), APP_STATUSBAR_HEIGHT, ScreenX375(22), ScreenX375(22))];
    [leftButton setImage:[UIImage imageNamed:@"返回红色"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    
    UIImageView *topImageView = [[UIImageView alloc] init];
    topImageView.centerX = self.view.centerX;
    topImageView.size = CGSizeMake(ScreenX375(80), ScreenX375(80));
    topImageView.left = (SCREEN_WIDTH - ScreenX375(80)) / 2;
    topImageView.top = NAVIGATION_BAR_HEIGHT - ScreenX375(5);
    topImageView.image = [UIImage imageNamed:@"组11"];
    [self.view addSubview:topImageView];
    
    self.textLabel = [[UILabel alloc] init];
    self.textLabel.frame = CGRectMake((SCREEN_WIDTH - ScreenX375(200))/2, topImageView.bottom, ScreenX375(200), ScreenX375(20));
    self.textLabel.font = [UIFont systemFontOfSize:18];
    self.textLabel.textColor = RGB_COLOR(51, 51, 51);
    
    if (self.registerType == InitRegisterType_Personal) {
        self.textLabel.text = @"注册成为设计师";
        _is_Person = YES;
    }
//    else{
//        _is_Person = NO;
//        switch (self.finalRegType) {
//            case FinalRegisterType_Designer:
//            {
//                self.textLabel.text = @"注册成为设计师";
//            }
//                break;
//            case FinalRegisterType_Manerger:
//            {
//                self.textLabel.text = @"注册成为包装经理人";
//            }
//                break;
//            case FinalRegisterType_Customer:
//            {
//                self.textLabel.text = @"注册成为终端客户";
//            }
//                break;
//            default:
//                break;
//        }
//    }
    
    self.textLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.textLabel];
    
    self.progressView = [[UIImageView alloc] init];
    self.progressView.frame = CGRectMake(ScreenX375(30), self.textLabel.bottom + ScreenX375(20), SCREEN_WIDTH - ScreenX375(60), ScreenX375(26));
    self.progressView.contentMode = UIViewContentModeCenter;

    self.progressView.image = [UIImage imageNamed:@"未完成"];
    [self.view addSubview:self.progressView];
    
    self.loginButton = [[UIButton alloc] init];
    
    self.loginButton.frame = CGRectMake(SCREEN_WIDTH - ScreenX375(80), NAVIGATION_BAR_HEIGHT - ScreenX375(10), ScreenX375(65), ScreenX375(30));
    self.loginButton.layer.masksToBounds = true;
    self.loginButton.layer.cornerRadius = 15;
    self.loginButton.layer.borderWidth = 0.5;
    self.loginButton.layer.borderColor = RGB_COLOR(237, 34, 59).CGColor;
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginButton addTarget:self action:@selector(LoginClick) forControlEvents:UIControlEventTouchUpInside];
    self.loginButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.loginButton setTitleColor:RGB_COLOR(237, 34, 59) forState:UIControlStateNormal];
    [self.view addSubview:self.loginButton];
    
}

#pragma mark - 设置中间部分View
-(void)setPersonalRegisterUI
{
    self.regisView = [[FPRegisAndForgetView alloc] init];
    self.regisView.isMsgRegist = YES;
    self.regisView.frame = CGRectMake(0, self.progressView.bottom, SCREEN_WIDTH, ScreenX375(236));// + ScreenX375(10)
    [self.view addSubview:self.regisView];
    
    self.nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextButton.backgroundColor = RGB_COLOR(237, 34, 59);
    self.nextButton.frame = CGRectMake(ScreenX375(40), self.regisView.bottom + ScreenX375(40), SCREEN_WIDTH - ScreenX375(80), ScreenX375(44));
    self.nextButton.layer.masksToBounds = true;
    self.nextButton.layer.cornerRadius = 22;
    [self.nextButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.nextButton setTitle:@"下一步" forState:UIControlStateNormal];
    [self.nextButton addTarget:self action:@selector(nextClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.nextButton];
    
}

#pragma mark - 设置尾部三方登录View
-(void)setBottomViewUI
{
    FPThirdView *thirdView = [[FPThirdView alloc] init];
    
    thirdView.frame = CGRectMake(0, SCREEN_HEIGHT - ScreenX375(100), SCREEN_WIDTH, ScreenX375(100));
    [self.view addSubview:thirdView];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:true];
}



@end
